package com.ChefApp.mobile;

import java.io.IOException;
import java.util.Vector;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import android.app.ListActivity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

public class Results<T> extends ListActivity{
	Elements items;
	
	Vector<ListItemWithImage> imageItem;
	String nextUrl,county;
	View footer;
	
	
	public void onCreate(Bundle icicle){
		super.onCreate(icicle);
		Bundle cicle = getIntent().getExtras();
		items = getItems(cicle.getString("url"));
		
		imageItem = new Vector<ListItemWithImage>();
		
		
        
		String currUrl=cicle.getString("url");
		int newCount = Integer.valueOf((String) cicle.get("count")).intValue()+30;
		county= ""+newCount;
		nextUrl=currUrl.replaceAll("[0-9]+", county);
		try {
			searchItems(items,"title");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		@SuppressWarnings("unused")
		String ahit="";
		
		ViewGroup footerViewOriginal = (ViewGroup) findViewById(R.id.butt);
		footer = getLayoutInflater().inflate(R.layout.footer,footerViewOriginal);
		footer.findViewById(R.id.butt).setOnTouchListener(new View.OnTouchListener() {
			
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				Intent inty = new Intent(v.getContext(),Results.class);
				inty.putExtra("url", nextUrl);
				inty.putExtra("count", county);
				startActivity(inty);
				return false;
			}
		});
		
		ListView lv =getListView();
		lv.addFooterView(footer,null,true);
		lv.setBackgroundDrawable(getResources().getDrawable(R.drawable.bluegrad));
		
		ImageItemAdapter itemAdapter = new ImageItemAdapter(getApplicationContext(), R.layout.image_row, imageItem);
//		setListAdapter(new ArrayAdapter<T>(this, R.layout.resrow, R.id.textViewRes1, (T[]) titles.toArray()));
		setListAdapter(itemAdapter);
		}
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		// Get the item that was clicked
		@SuppressWarnings("unused")
		Object o = this.getListAdapter().getItem(position);
		
		Intent showrec= new Intent(l.getContext(),Receipe.class);
				showrec.putExtra("url", imageItem.get(position).getUrl());
				showrec.putExtra("title", imageItem.get(position).getTitle());
				showrec.putExtra("subtitle", imageItem.get(position).getSubtitle());
				showrec.putExtra("image", imageItem.get(position).getBild());
				showrec.putExtra("type", "list");
				showrec.putExtra("imageurl", imageItem.get(position).getImageUrl());
				startActivity(showrec);
	}
	
	
	
	
	private void searchItems(Elements items2, String type) throws Exception {
		// TODO Auto-generated method stub
		if (type.equals("title")){
			
			for (Element element : items2) {
				String title=element.child(1).child(0).text();
				String subtitle = element.child(1).child(3).text();
				String imageUrl = element.child(0).child(0).attr("src");
				String url= "http://www.chefkoch.de"+element.child(1).child(0).attr("href");
				Bitmap bild = MyImageLoader.LoadImage(imageUrl);
				String bewertungUrl = element.child(2).child(0).attr("src");
				String date = element.child(1).getElementsByClass("s").first().text();
				String aufwand = element.child(3).text();
				Log.v("subs", subtitle);
				Log.v("bg", bewertungUrl);
				imageItem.add( new ListItemWithImage(bild, title, url, subtitle, imageUrl, bewertungUrl,date,aufwand));
			}
		}
		
	}


	



	private Elements getItems(String url) {
		// TODO Auto-generated method stub
		Document page=null;
		
	    try {
	    	
			page = Jsoup.connect(url).userAgent("Mozilla").get();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		Elements items = page.getElementsByAttributeValue("onMouseOut","this.bgColor='#ffffff'");
		
		return items;
	}
}
